package cn.wsgfz.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import cn.wsgfz.SizeOfHandler;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;

public class DifficultDataUtils {
	public static final String res_size_name = "资源大小";
	public static final String use_memory_name = "使用内存";
	public static final String use_cpu_name = "使用CPU效率";
	
	public static int getBitmapMemory(int id, Context ctx) {
		
		int memory = 0;
		Resources res = ctx.getResources();
		Drawable draw = res.getDrawable(id);
		Bitmap bitmap = ((BitmapDrawable)draw).getBitmap();
		
		if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
			memory = bitmap.getRowBytes() * bitmap.getHeight();
		} else {
            memory = bitmap.getRowBytes();
		}
        return memory;
		
//		Resources res = ctx.getResources();
//		Bitmap bitmap = BitmapFactory.decodeResource(res, id);
//		BitmapDrawable bd = new BitmapDrawable(bitmap);
////		return getObjMemory(bitmap);
//		return writeObject(new SerializableDrawable(bd));
	}
	
	private static int writeObject(Object obj) {
		String name = obj.getClass().getSimpleName() + "22.txt";
		try {
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
				File sdCardDir = Environment.getExternalStorageDirectory();
				String fPath = sdCardDir.getAbsoluteFile() + File.separator + name;
				
				File file = new File(fPath);
				
				if(!file.exists()) {
					file.createNewFile();
				}
				
				ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fPath));
				oos.writeObject(obj);
				oos.close();
				
				long length = file.length();
				if(file.exists()) {
					file.delete();
				} 
				
				return (int)length;
			}
		} catch(IOException ex) {
			ex.printStackTrace();
		}
		
		return 0;
	}
	
	public static Picture getPicture(int rawId, Context ctx) {
		SVG svg = SVGParser.getSVGFromResource(ctx.getResources(), rawId);
		Picture p = svg.getPicture();
		return p;
	}
	
	public static int getPictureDrwaMemory(int rawId, Context ctx, float scale) {
//		return getObjMemory(p);
//		PictureDrawablePlus pdp = new PictureDrawablePlus(getPicture(rawId, ctx), scale);
//		return writeObject(new SerializableDrawable(pdp));
//		return getObjMemory();
		
		try {
			return new SizeOfHandler(ctx, rawId, scale).size();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
//		try {
//			
//			List<Object> list = new ArrayList<Object>();
//			long begin = Runtime.getRuntime().totalMemory();
//			int index = 0;
//			while(true) {
//				SVG svg = SVGParser.getSVGFromResource(ctx.getResources(), rawId);
//				Picture p = svg.getPicture();
//				list.add(new PictureDrawablePlus(svg.getPicture(), 1.0f));
//				Log.e("runtime", "index:" + index + "," + (float)(Runtime.getRuntime().totalMemory() - begin / (++index) / 1024) + "");
//			}
//			
//			
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		return 0;
		
//		long beforeMemory = Runtime.getRuntime().totalMemory();
//		long afterMemory  = Runtime.getRuntime().freeMemory();
//		return (int)(beforeMemory - afterMemory) / 1024;
	}
}
